package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.filechooser.FileNameExtensionFilter;

import models.ModelListImport;
import models.ModelMyPlayer;

import org.ini4j.Ini;

import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import variables.VarConfigInt;

public class ActionOpenListener implements ActionListener {

	protected JFrame jframe;
	protected EmbeddedMediaPlayer mediaPlayer;
	protected File directory;
	protected ModelMyPlayer myPlayer;
	protected ModelListImport listImport;
	protected Ini configIni;
	protected JSlider volumeSlider;
	
	public ActionOpenListener(JFrame jframe, EmbeddedMediaPlayer mediaPlayer,
			ModelMyPlayer myPlayer, ModelListImport listImport, JSlider volumeSlider, Ini configIni) {
		this.mediaPlayer = mediaPlayer;
		this.jframe = jframe;
		this.myPlayer = myPlayer;
		this.listImport = listImport;
		this.volumeSlider = volumeSlider;
		this.configIni = configIni;
	}

	public void actionPerformed(ActionEvent arg0) {
		// On ouvre directement le répertoire vidéo
		this.directory = new File(configIni.get(
				VarConfigInt.CONFIG_SECTION_PATH,
				VarConfigInt.CONFIG_VAR_PATH_MOVIES));
		JFileChooser chooser = new JFileChooser(this.directory);
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
				"Videos files", "avi", "mkv", "wmv", "mp4", "m4v");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(this.jframe);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String chemin = chooser.getSelectedFile().getAbsolutePath();
			try {
				chemin = new String(chemin.getBytes(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			this.listImport.setLink(chemin);
			this.myPlayer.setSrc(chemin);
			this.volumeSlider.setValue(this.mediaPlayer.getVolume());
		}
	}
}
